<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script type="text/javascript" src="js/promise.js"></script>
</head>
<body>
<script type="text/javascript">

    // const p = new Promise((resolve, reject) => {
    //     // setTimeout(() => {
    //         resolve("OK");
    //     //     reject("Error");
    //         // throw "Error1";
    //     // }, 1000);
    // });

    // console.log(p);

    // let res = p.then((val) => {
    //     console.log(val);
    //     // return new Promise((resolve, reject) => {
    //         // resolve('success');
    //     //     reject("rejected");
    //     // })
    //     // throw "Error";
    // }, (err) => {
    //     console.warn(err);
    // });
    //
    // console.log(res);

    // p.catch((err) => {
    //     console.warn(err);
    // });

    // 异常穿透
    // p.then((val) => {
    //     console.log(1);
    //     throw "Error1";
    // }).then((val) => {
    //     console.log(2);
    // }).then((val) => {
    //     console.log(3);
    // }).catch((err) => {
    //     console.warn(err);
    // });

    // 值传递
    // p.then().then((val) => {
    //     console.log(2);
    // }).then((val) => {
    //     console.log(3);
    // }).catch((err) => {
    //     console.warn(err);
    // });

    // Promise.resolve()
    /*const p1 = Promise.resolve('OK');
    console.log(p1);

    const p2 = Promise.resolve(new Promise((resolve, reject) => {
        // resolve("OK");
        reject("Error");
    }));
    console.log(p2);

    const p3 = Promise.resolve(Promise.resolve("p3"));
    console.log(p3);*/

    // Promise.reject()
    /*const p1 = Promise.reject("Error");
    console.log(p1);

    const p2 = Promise.reject(Promise.resolve("OK"));
    console.log(p2);

    const p3 = Promise.reject(new Promise((resolve, reject) => {
        throw "Err";
    }));
    console.log(p3);*/

    // Promise.all()
    /*const p1 = Promise.resolve('p1');
    const p2 = Promise.resolve(new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("p2");
            // reject("p2");
        }, 1000);
    }));
    const p3 = Promise.resolve(Promise.resolve("p3"));
    const pall = Promise.all([p1, p2, p3]);
    console.log(pall);*/

    // Promise.race()
    /*const p1 = Promise.resolve(new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("p1");
            // reject("p1");
        }, 1000);
    }));
    const p2 = Promise.reject('p2');
    const p3 = Promise.resolve(Promise.resolve("p3"));
    const prace = Promise.race([p1, p2, p3]);
    console.log(prace);*/

    // 回调函数异步执行
    /*const p1 = Promise.resolve(new Promise((resolve, reject) => {
        // setTimeout(() => {
        resolve("p1");
        // reject("p1");
        console.log(1)
        // }, 1000);
    }));
    let res = p1.then((val) => {
        console.log(2);
    },(err)=>{
        console.warn(2);
    });
    console.log(res);
    console.log(3);*/

    console.log(Promise.resolve("OK"));
</script>
</body>
</html>